package com.nchu.commondevelopmentframework.common.storage;

import com.nchu.commondevelopmentframework.common.storage.config.StorageProperties;
import com.nchu.commondevelopmentframework.common.storage.impl.AliYunStorageImpl;
import com.nchu.commondevelopmentframework.common.storage.impl.LocalStorageImpl;
import com.nchu.commondevelopmentframework.common.storage.impl.MinioStorageImpl;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @className: StorageFactory
 * @description: 存储工厂
 * @author: Li Chuanwei
 * @date: 2024/01/22 21:20
 * @Company: Copyright [日期] by [作者或个人]
 **/
@Component
public class StorageFactory implements InitializingBean {
    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private StorageProperties storageProperties;
    /**
     * 对应Mapper
     */
    private Map<String, StorageStrategy> storageMap = new HashMap<String, StorageStrategy>();

    public StorageStrategy get() {
        String scheme = storageProperties.getScheme();
        if (StringUtils.isBlank(scheme)) {
            scheme = "local";
        }
        System.out.println(scheme);
        return storageMap.get(scheme);
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        storageMap.put("AliYun", applicationContext.getBean(AliYunStorageImpl.class));
        storageMap.put("minio", applicationContext.getBean(MinioStorageImpl.class));
        storageMap.put("local", applicationContext.getBean(LocalStorageImpl.class));
    }
}
